package com.facebook.app.client.uiElements;

import java.util.List;

import com.facebook.app.shared.facebookData.FacebookData;
import com.facebook.app.shared.facebookData.FacebookFriend;
import com.facebook.app.shared.facebookData.FacebookLike;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.TreeViewModel;

public class SelectedFriendsTreeModel implements TreeViewModel {
	
	private final SelectionModel<FacebookData> selectionModel;
	private final List<FacebookData> selectedFriends;
	
	public SelectedFriendsTreeModel(List<FacebookData> selectedFriends, SelectionModel<FacebookData> selectionModel) {
		this.selectedFriends = selectedFriends;
		this.selectionModel = selectionModel;
	}
	
	@Override
	public <T> NodeInfo<?> getNodeInfo(T value) {
		
		if (value == null) {
			ListDataProvider<FacebookData> dataProvider = new ListDataProvider<FacebookData>(selectedFriends);
			
			return new DefaultNodeInfo<FacebookData>(dataProvider, new FacebookDataCell());
		} else if (value instanceof FacebookFriend) {
			ListDataProvider<FacebookData> dataProvider = new ListDataProvider<FacebookData>();
			
			dataProvider.getList().addAll(((FacebookFriend) value).getLikes());
			
			return new DefaultNodeInfo<FacebookData>(dataProvider, new FacebookDataCell(), selectionModel, null);
		}
		
		throw new IllegalArgumentException("Unsupported object type: " + value.getClass().getName());		
	}
	
	public boolean isLeaf(Object value) {
		return value instanceof FacebookLike;
	}

}
